<template>
  <div class="app-container">
    <div style="display: flex; margin-bottom: 20px">
      <div>
        <el-input
          v-model="params.title"
          size="big"
          :clearable="true"
          placeholder="请输入新闻咨询类型"
        />
      </div>
      <div style="margin-left: 20px">
        <el-button
          type="primary"
          size="big"
          icon="el-icon-search"
          @click="searchList"
          >搜索</el-button
        >
      </div>
      <div style="margin-left: 20px">
        <el-button type="primary" size="big" @click="showAdd()"
          >添加新闻咨询类型</el-button
        >
      </div>
    </div>
    <el-dialog title="添加新闻咨询类型" :visible.sync="dialogFormVisible">
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item label="新闻咨询类型" prop="title">
          <el-input v-model="form.title" placeholder="请输入新闻咨询类型" />
        </el-form-item>
        <el-form-item label="咨询类型简介" prop="desc">
          <el-input v-model="form.desc" placeholder="请输入新闻咨询类型简介" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="doAdd('form')">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="新闻咨询类型" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="新闻咨询类型简介" width="300" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.desc }}</span>
        </template>
      </el-table-column>
      <el-table-column
        class-name="status-col"
        label="状态"
        width="100"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status | statusFilter"
            v-if="scope.row.status == 1"
            >正常</el-tag
          >
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="created_at"
        label="创建时间"
        width="200"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.created_at }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="updated_at"
        label="更新时间"
        width="200"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.updated_at }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="350">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="eidtInfo(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="deleteCate(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改新闻咨询类型" :visible.sync="dialogForm">
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item label="新闻咨询类型" prop="title">
          <el-input v-model="form.title" placeholder="请输入新闻咨询类型" />
        </el-form-item>
        <el-form-item label="咨询类型简介" prop="desc">
          <el-input v-model="form.desc" placeholder="请输入新闻咨询类型简介" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" @click="doEdit('form')">确 定</el-button>
      </div>
    </el-dialog>
    <el-pagination
      align="center"
      background
      :current-page="params.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="params.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 20px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getCate } from "@/api/cate";
import { addCate } from "@/api/cate";
import { editCate } from "@/api/cate";
import { delCate } from "@/api/cate";
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialogFormVisible: false,
      dialogForm: false,
      form: {
        id: "",
        title: "",
        desc: "",
      },
      params: {
        title: "",
        page: 1,
        limit: 10,
      },
      total: "",
      rules: {
        title: [
          { required: true, message: "请输入新闻咨询类型", trigger: "blur" },
        ],
        introduce: [
          {
            required: true,
            message: "请输入新闻咨询类型简介",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    //显示修改
    eidtInfo(info) {
      this.dialogForm = true;
      //this.form = info;
      this.form.id = info.id;
      this.form.title = info.title;
      this.form.desc = info.desc;
    },
    //确定修改
    doEdit(formName) {
      editCate(this.form).then((res) => {
        if (res.code == 20000) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.dialogForm = false;
          this.fetchData();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    //显示添加
    showAdd() {
      this.dialogFormVisible = true;
    },
    //添加
    doAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addCate(this.form).then((res) => {
            if (res.code == 20000) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.dialogFormVisible = false;
              this.fetchData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        }
      });
    },
    //删除
    deleteCate(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delCate(id).then((res) => {
            if (res.code == 20000) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.fetchData();
            } else {
              this.$message({
                message: "删除失败",
                type: "error",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分页
    handleSizeChange(val) {
      this.params.limit = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.params.page = val;
      this.fetchData();
    },
    //搜索
    searchList() {
      this.fetchData();
    },
    fetchData() {
      this.listLoading = true;
      getCate(this.params).then((response) => {
        this.list = response.data.data;
        this.list.total = response.data.total;
        this.listLoading = false;
        this.total = response.data.total;
      });
    },
  },
};
</script>
